// pages/addWeChat/index.js
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    btntext: '获取验证码',
    isSend: false,
    weChatNumber:"",
    weChatNickname:"",
    phoneNumber: "",
    verificationCode: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      phoneNumber: util.isLogin().mobile
    })
  },

  weChatNumberInput: function (e) {
    this.setData({
      weChatNumber: e.detail.value
    })
  },

  weChatNicknameInput: function (e) {
    this.setData({
      weChatNickname: e.detail.value
    })
  },

  phoneNumberInput: function (e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },

  verificationCodeInput: function (e) {
    this.setData({
      verificationCode: e.detail.value
    })
  },

  getverificationCode: function () {
    if (this.data.isSend) {
      return;
    }
    var that = this;
    if (this.data.phoneNumber.length == 11 && this.data.phoneNumber.indexOf('1') == 0) {
      that.setData({
        isSend: true
      })

      let data = {
        mobile: this.data.phoneNumber,
        event: 'validate'
      }
      util.http({
        url: '/api/miniwechat/user/sendSms',
        method: 'POST',
        data
      }).then(res => {

        console.log(res);
        var restTimeStamp = new Date().getTime() + 60 * 1000;
        wx.setStorageSync('REST_COUNT_DOWN', restTimeStamp);
        countDown_60s(that, restTimeStamp);
      }).catch(ex => {
        wx.showToast({
          title: String(ex),
          icon: 'none',
          duration: 2000
        })
      })
    } else {
      wx.showToast({
        title: "请输入正确手机号！",
        icon: 'none',
        duration: 2000
      })
    }
  },

  submit: function () {
    let userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo);
    let data = {
      user_id: userInfo.user_id,
      mobile: this.data.phoneNumber,
      captcha: this.data.verificationCode,
      wechat_account: this.data.weChatNumber,
      wechat_nickname: this.data.weChatNickname,
      event: "validate"
    }
    util.http({
      url: '/api/miniwechat/center/bank_info',
      method: 'POST',
      headers: {
        token: userInfo.token
      },
      data
    }).then(res => {
      wx.showToast({
        title: '添加成功',
        icon: 'none',
        duration: 2000
      })
      wx.navigateBack({
        delta: 1
      })
    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

function countDown_60s(_this, restTimeStamp) {
  var nowtimeStamp = new Date().getTime();
  var coden = parseInt((restTimeStamp - nowtimeStamp) / 1000); // 定义60秒的倒计时
  if (coden > 0) {
    _this.setData({
      isSend: true
    })
    _this.data.codeV = setInterval(function () {
      _this.setData({
        btntext: (--coden) + 's'
      })
      if (coden < 0) { // 清除setInterval倒计时，这里可以做很多操作，按钮变回原样等
        clearInterval(_this.data.codeV);
        wx.removeStorageSync('REST_COUNT_DOWN');
        _this.setData({
          isSend: false,
          btntext: '重新发送'
        })
      }
    }, 1000) //  1000是1秒
  } else {
    _this.setData({
      isSend: false
    })
  }
}